package com.willren.nutrss;

import com.willren.nutrss.GoogleReaderApi.GoogleReaderApiInterface;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class NutRssActivity extends Activity {
	
	GoogleReaderApi googleApi;
	
	GoogleReaderApiInterface callbackLogin;
	GoogleReaderApiInterface callbackFeed;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        googleApi = GoogleReaderApi.getInstance("renxiaoying1984@gmail.com", "XiaoYao2012");
        
        callbackLogin = new GoogleReaderApiInterface() {
			@Override
			public void googleReadDataReceived(boolean result) {
				final boolean finalResult = result;
				callback(1);
				runOnUiThread(new Runnable() {
					public void run() {
						if (finalResult) {
							Toast.makeText(NutRssActivity.this, "login success", Toast.LENGTH_SHORT).show();
						}
						else {
							Toast.makeText(NutRssActivity.this, "login failed", Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
		};
		
		callbackFeed = new GoogleReaderApiInterface() {
			@Override
			public void googleReadDataReceived(boolean result) {
				final boolean finalResult = result;
				
				runOnUiThread(new Runnable() {
					public void run() {
						if (finalResult) {
							Toast.makeText(NutRssActivity.this, "feed success", Toast.LENGTH_SHORT).show();
						}
						else {
							Toast.makeText(NutRssActivity.this, "feed failed", Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
		};
        
        Button button = (Button) findViewById(R.id.button1);
        if (null != button) {
        	button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (null != googleApi) {
						googleApi.reqLogin(callbackLogin);
					}
				}
        	});
        }
    }
    
    private void callback(int i) {
    	if (1 == i) {
    		googleApi.reqFeeds(callbackFeed);
    	}
    }
}